//
//  SWKidEvent.swift
//  ChildStory
//
//  Created by linhan on 2016/11/11.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation

extension SWStageSummary
{
    convenience init(response:SWStageSummaryRsp)
    {
        self.init()
        title = response.title
        events = response.events?.map({SWKidEventSummary(response:$0)})
    }
}

extension SWKidEventSummary
{
    convenience init(response:SWEventSummaryRsp)
    {
        self.init()
        type = response.type
        count = response.count ?? 0
        content = response.content
        features = response.features?.map({SWKidEventFeatureSummary(response:$0)})
    }
}

extension SWKidEventFeatureSummary
{
    convenience init(response:SWEventFeatureSummaryRsp)
    {
        self.init()
        title = response.title
        featureID = "\(response.id ?? 0)"
    }
}

extension SWStage
{
    convenience init(response:SWStageRsp)
    {
        self.init()
        stageID = response.id
        title = response.title
        start = (response.start ?? "0").intValue
        end = (response.end ?? "0").intValue
        //index = response.index ?? 0
        //thumbURL = response.thumbURL
        //events = response.events?.flatMap({SWKidEvent(response:$0)})
    }
}

extension SWKidEvent
{
    convenience init?(response:SWBaseEventRsp)
    {
        self.init()
        guard let eventID = response.eventID,eventID != 0 else {
            return nil
        }
        self.eventID = "\(eventID)"
        title = response.title
        coverURL = response.coverURL
        isFavorite = (response.isFavorite ?? 0) == 1
        if let majorExampleURL = response.majorExampleURL
        {
            exampleImages = [majorExampleURL]
        }
        
        if let response = response as? SWEventRsp
        {
            features = response.features?.flatMap({SWKidEventFeature(response:$0)})
        }
        
    }
    
    convenience init?(eventID:String)
    {
        self.init()
        if eventID == ""
        {
            return nil
        }
        self.eventID = eventID
    }
    
}

extension SWKidDetailEvent
{
    convenience init?(detailResponse:SWEventDetailFetchRsp)
    {
        self.init()
        guard let event = detailResponse.event,let eventID = event.eventID,eventID != 0 else {
            return
        }
        self.eventID = "\(eventID)"
        title = event.title
        //exampleImages = event.exampleImages
        features = event.features?.flatMap({SWKidEventFeature(response:$0)})
    }
}

extension SWKidEventFeature
{
    convenience init(response:SWEventFeatureRsp)
    {
        self.init()
        self.title = response.title
        self.content = response.content
    }
}

